package com.nuanshui.framework.behavior;

/**
 * Created by zxh on 2016/11/20.
 */
import java.util.HashMap;
import java.util.Map;

/**
 * 线程上下文临时存储变量，为单例对象提供便利
 */
public class BehaviorContext {
    private static final ThreadLocal<BehaviorContext> LOCAL = new ThreadLocal() {
        protected BehaviorContext initialValue() {
            return new BehaviorContext();
        }
    };

    public static void removeContext() {
        LOCAL.remove();
    }

    /**
     * 取得上下文的内容.
     * @return
     */
    public static BehaviorContext getContext() {
        return LOCAL.get();
    }

    private Map<String, Object> context = new HashMap<String,Object>();

    /**
     * 初始化上下文环境.
     */
    public BehaviorContext() {

    }

    /**
     * 得到上下文.
     * @return
     */
    public Map<String, Object> getContextMap() {
        return context;
    }

    /**
     * 取得上下文中的值.
     * @param key
     * @return
     */
    public Object get(String key) {
        return context.get(key);
    }

    /**
     * 设置上下文中的值.
     * @param key
     * @return
     */
    public void put(String key, Object value) {
        context.put(key, value);
    }
}
